<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Blog/classes/PhpFox_ComponentBrowse.class.php, $Author:  $ */



/** Component for showing blogs
 *
 * @version $Id: PhpFox_ComponentBrowse.class.php 437 2008-04-18 15:51:35Z phpfoxsv $
 * @package module.blog
 */
class PhpFox_Mod_Blog_ComponentBrowse extends BaseComponent
{
    function process()
    {
        $oBlogItem = $this->_oModule->getItem('Blog');
        /* @var $oBlogItem PhpFox_Mod_Blog_ItemBlog */
        $oSecurityService = App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oBlogService = &$this->_oModule->getService('Blog');
        /* @var $oBlogService PhpFox_Mod_Blog_ServiceBlog */
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        $oTpl = $this->_oModule->getTemplate();
        App::loadClass('Filter');
        App::loadClass('Sorter');
        App::loadClass('Pager');
        $oText = App::newObject('Text');
        /* @var $oText Text */

        $oSecurityService->checkPerm('blog', array('read_only', 'full'));
        $oSecurityService->checkAccess('blogs');
        $aPageSizes = array(5, 11, 17);
        $aForm = array();
        $aForm['field']    = $oReq->get('field', 'time');
        $aForm['display']  = $oReq->get('display', 1);
        $aForm['pagesize'] = $oReq->getInt('pagesize', 5);
        if (!in_array($aForm['pagesize'], $aPageSizes))
        {
        	$aForm['pagesize'] = 5;
        }
        $aForm['mode']     = $oReq->getInt('mode', 1);
        $oUrl->setParam('field', $aForm['field']);
        $oUrl->setParam('display', $aForm['display']);
        $oUrl->setParam('pagesize', $aForm['pagesize']);
        $oUrl->setParam('mode', $aForm['mode']);
        if ($iId = $oReq->getInt('delete'))
        {
            $oBlogService->deleteBlog($iId);
            $oUrl->clearParams('delete', 1);
            App::forward($oUrl->getUrl(), App::format('Mod_Blog.blog_deleted'), 'message', App::getPageName(), 'BlogList');
        }
        $iPageSize = $aForm['pagesize'];
        $iPage = $oReq->getInt('page', 1);

        $aFilter = array();
        $oFilter = &new Filter('blogbrowse', $aFilter);
        $aSort = array
        (
        	'time',
        	'view',
        	'comment',
        );
        $aDefSort = array($oReq->get('field', 'time') => 'desc');
        $oSorter = &new Sorter($aSort, $aDefSort);
        $aCond = array();
        foreach ($oFilter->aConditions as $sCond)
        {
            $aCond[] = $sCond;
        }

        if ( $tag = $oReq->get('tag') )
        {
        	$oUrl->setParam('tag', strip_tags($tag));
        	$aCond[] = " j.tags LIKE '%". $this->oDatabase->escape($tag) ."%' ";
        }

        if ( $sSearchQuery = $oReq->get('search') )
        {
        	$search = str_replace("-", " ", $sSearchQuery);
        	$oUrl->setParam('search', strip_tags($sSearchQuery));
			$aCond[] = " (j.title LIKE '%". $this->oDatabase->escape(strip_tags($sSearchQuery)) ."%' OR j.text LIKE '%". $this->oDatabase->escape(strip_tags($sSearchQuery)) ."%') ";
        }

		$aJoin = array();
		$aJoin[] = " ". App::getT('user') ." AS u ON(j.user_id = u.id) ";

		list($aItems, $iCnt) = $oBlogItem->getList($aCond, $iPage, $iPageSize, $oSorter->getOrder(), '', '', 'j.id, j.title, j.text, j.time, j.comment, u.user', $aJoin);

		foreach ($aItems as $iKey => $aItem)
		{
	    	$aItem['text'] = $oText->_splitString($aItem['text'], 30);
        	$aItem['text'] = strip_tags($aItem['text'], '<br>, <br />, <p>');
			$aItems[$iKey]['text']    = $oText->ShortenText($aItem['text'], 500, '<br />');
		    $aItems[$iKey]['is_long'] = (strlen($aItem['text']) > 100) ? true : false;
			if ( isset($tag) )
		    {
		    	$aItems[$iKey]['text']    = $oText->_createHighlight($aItems[$iKey]['text'], $tag, App::makeUrl('public.blog.view',array('id' => $aItems[$iKey]['id'], 'title' => strip_tags($aItems[$iKey]['id']))));
		    }
		}
		$oPager = &new Pager($iCnt, $iPage, $iPageSize);
		$iFrom = $oPager->getOffset($iPage, $iPageSize);
		$iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

		$oTpl->assignSrc('aItems', $aItems);
		$oTpl->assign(array(
        	'aFilter'          => $oFilter->aHtml,
        	'iCnt'             => $iCnt,
			'sUImageUrl'       => App::getParam('user.pic.url'),
        	'iFrom'            => ($iCnt) ? ($iFrom + 1) : 0,
        	'aForm'            => $aForm,
		    'bCanComment'      => $oSecurityService->checkPerm('blog', array('full'), false),
			'aPaging'          => ($iCnt) ? $oPager->getInfo($oUrl) : '',
			'bAdmin'           => $oSecurityService->isStaff(),
        	'iTo'              => $iTo,
        	'sTag' => isset($tag) ? strip_tags($tag) : '',
        ));
        return $oTpl->fetch('BlogList.html');
    }
}
?>